﻿using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using CommonLibrary.Models;
using ShuntingScheduleUserControl.Helper;
using ShuntingScheduleUserControl.Model;

namespace ShuntingScheduleUserControl.View
{
    public class ViewTrain
    {
        public int JobId { get; }
        private readonly DrawContext DrawContext;
        public List<ViewShuntingSchedule> ViewSchedulesByJobId { get; private set; } = new List<ViewShuntingSchedule>();
        public bool IsShow { get; set; } = true;

        public ViewTrain(int jobId, List<Schedule> list, DrawContext drawContext)
        {
            JobId = jobId;
            DrawContext = drawContext;

            CalculateShapes(list);
        }

        private void CalculateShapes(List<Schedule> shuntingSchedules)
        {
            ViewSchedulesByJobId = new List<ViewShuntingSchedule>();

            for (var index = 0; index < shuntingSchedules.Count; index++)
            {
                var shuntingSchedule = shuntingSchedules[index];
                ViewSchedulesByJobId.Add(new ViewShuntingSchedule(index, shuntingSchedules.Count, shuntingSchedule, DrawContext));
            }
        }

        public void Draw(Graphics g, Rectangle rec)
        {
            if (!IsShow) return;
            //绘制任务时间块
            foreach (var viewShuntingSchedule in ViewSchedulesByJobId)
            {
                viewShuntingSchedule.Draw(g);
            }

            if (!DrawContext.DrawConnectionLine) return;
            //绘制车组任务间的连接线
            for (int i = 0; i < ViewSchedulesByJobId.Count - 1; i++)
            {
                var schedule1 = ViewSchedulesByJobId[i];
                var schedule2 = ViewSchedulesByJobId[i + 1];

                g.DrawLine(DrawHelper.PenBlack1Dot, schedule1.XRight, schedule1.Y, schedule2.XLeft, schedule2.Y);
            }
        }

        public bool IsInRange(Point p)
        {
            return ViewSchedulesByJobId.Any(x => x.IsInRange(p));
        }
    }
}